--[[
Author: your name
Date: 2020-12-05 16:29:30
LastEditTime: 2021-01-19 17:50:33
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \Lua\Battle\Entity\LineEffect.lua
--]]
require "Battle/Manager/SpineEffectManager"

LineEffect = {}
local LineEffect = LineEffect
local mt = {__index = LineEffect}
local offset = 1.8; 
local offsetY = Mathf.Cos(Mathf.PI/6) * offset;
local offsetZ = Mathf.Sin(Mathf.PI/6) * offset;

function LineEffect:new(name,parent,target,duration)
	return setmetatable({name = name, parent=parent,target=target,duration = duration}, mt)
end

function LineEffect:start()
    --log("SpineEffect:start:"..actionName);
	if not self.handle then
		self.handle = UpdateBeat:Add(self.update, self);
    end
    
    local subName = string.split(self.name,"/");
	local effGo =  SpineEffectManager.getEffect(subName[#subName])
	--静态脚本没有填特效全路径
	local i,j = string.find(self.name,"/");
	local effName = self.name;
    self.ending = false;
    
    if effGo == nil then
        resMgr:LoadModelPrefab("Prefabs/Skill/"..effName,function(obj1)
            local skillObj = newObject(obj1);
            Util.Lua_SetLocalScaleXYZ(skillObj,1,1,1)
            Util.Lua_SetLocalRotationXYZ(skillObj,40,0,0)

            if self.parent ~= nil then
                skillObj.transform:SetParent(self.parent);
                self.position = Vector3(0,offsetY,offsetZ);
            end
            Util.Lua_SetLocalPositionXYZ(skillObj,self.position.x,self.position.y,self.position.z)
            local linerender = skillObj.transform:GetComponent("LineRender");
            self.effGo = skillObj;
            self.effGo.name = skillObj.name;  
            self.effGo:SetActive(true);
        end);
	else
		self.effGo = effGo
        Util.Lua_SetLocalScaleXYZ(self.effGo.transform,1,1,1)
        Util.Lua_SetLocalRotationXYZ(self.effGo.transform,40,0,0)

        if self.parent ~= nil then
            self.effGo.transform:SetParent(self.parent);
            self.position = Vector3(0,offsetY,offsetZ);
        end
        Util.Lua_SetLocalPositionXYZ(self.effGo,self.position.x,self.position.y,self.position.z)
        self.effGo:SetActive(true);
    end
end

function LineEffect:stop()
    self.ending = true;
    self.effGo:SetActive(false);
	if self.handle then
		UpdateBeat:Remove(self.update, self); 
		self.handle = nil;
	end
	self.speed = nil;
	if self.effGo ~= nil then
		SpineEffectManager.releaseEffect(self.effGo, self.name)
		self.effGo = nil;
	end
end

function LineEffect:update()
	if self.ending or self.effGo == nil  then
		return
    end
    self.duration = self.duration - Time.deltaTime;
    if self.duration < 0 then
		self:stop();
		return;
	end
    
    if self.effGo ~= nil then
        local pos = self.parent.localPosition + Vector3(0,offsetY,offsetZ);
        local target_pos = self.target.localPosition + Vector3(0,offsetY,offsetZ);
        local linerender = self.effGo.transform:GetComponent("LineRenderer");
        linerender:SetPosition(0,pos);
        linerender:SetPosition(1,target_pos);
    end
	
	if self.effGo == nil then
		return;
	end
end
